﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoyoEden.Models;
using CoyoEden.Web.Data;
using CoyoEden.Web;
using CoyoEden.Web.Controls;
using System.IO;
using System.Web.UI;
using System.Web;

namespace CoyoEden.Web.Controller
{
	public class PostController:ControllerBase
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="Params"></param>
		/// <param name="msg"></param>
		/// <remarks>Used by Webmethod,renderring a view that doesn't depend on an existing page instance</remarks>
		public static void SelectPosts(Dictionary<string,string> Params,out FeedbackData msg ) {
			msg = new FeedbackData();
			var currentPage = Convert.ToInt32(Params["CPage"]);
			var rowPerPage = Convert.ToInt32(Params["RPPage"]);
			var categoryID = Convert.ToInt32(Params["CategoryID"]);
			var itemViewPath = Params["ViewPath"];

			var allItems = new List<Post>();
			allItems = Post.GetPostsByCategory(categoryID);
			//do paging
			SimplePager pager = new SimplePager();
			List<Post> items = null;
			if (allItems != null && allItems.Count > 0)
			{
				pager.PagedList<Post>(allItems, rowPerPage, currentPage, out items);
				//Return first record's id!!.For the purpose of locating the latest record
				msg.Context.Add("TopItemID", items[0].Id);
			}
			else
			{
				pager.CurrentPage = currentPage;
				pager.RowPerPage = rowPerPage;
				pager.RowTotal = 0;
				pager.PageTotal = 0;
				pager.Tip = "没有任何记录可以显示";
			}


			//Load Page view
			ViewManager<SimplePagerViewBase> viewObj = new ViewManager<SimplePagerViewBase>("~/Views/SimplePager.ascx");
			viewObj.Control.Pager = pager;
			msg.Context.Add("PagerHtml", viewObj.Render());

			//Load ITEM view
			ViewManager<CategoryViewBase> itemView = new ViewManager<CategoryViewBase>(itemViewPath);
			itemView.Control.PostsShow = items;

			msg.Context.Add("CategoryPostListHtml", itemView.Render());

			msg.Context.Add("Pager", pager);

		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="Params"></param>
		/// <param name="page"></param>
		/// <param name="msg"></param>
		/// <remarks>Used by page or control's Callback,renderring a view that depends on an existing page instance</remarks>
		public static void SelectPosts(Dictionary<string, string> Params,System.Web.UI.Page page, out FeedbackData msg)
		{
			msg = new FeedbackData();
			var currentPage = Convert.ToInt32(Params["CPage"]);
			var rowPerPage = Convert.ToInt32(Params["RPPage"]);
			var categoryID = Convert.ToInt32(Params["CategoryID"]);
			var itemViewPath = Params["ViewPath"];

			var allItems = new List<Post>();
			allItems = Post.GetPostsByCategory(categoryID);
			//do paging
			SimplePager pager = new SimplePager();
			List<Post> items = null;
			if (allItems != null && allItems.Count > 0)
			{
				pager.PagedList<Post>(allItems, rowPerPage, currentPage, out items);
				//Return first record's id!!.For the purpose of locating the latest record
				msg.Context.Add("TopItemID", items[0].Id);
			}
			else
			{
				pager.CurrentPage = currentPage;
				pager.RowPerPage = rowPerPage;
				pager.RowTotal = 0;
				pager.PageTotal = 0;
				pager.Tip = "没有任何记录可以显示";
			}


			//Load Page view
			ViewManager<SimplePagerViewBase> viewObj = new ViewManager<SimplePagerViewBase>("~/Views/SimplePager.ascx");
			viewObj.Control.Pager = pager;
			msg.Context.Add("PagerHtml", viewObj.Render());

			//Load ITEM view
			ViewManager<CategoryViewBase> itemView = new ViewManager<CategoryViewBase>(itemViewPath,page);
			itemView.Control.PostsShow = items;
			msg.Context.Add("CategoryPostListHtml", itemView.Render());

			msg.Context.Add("Pager", pager);

		}
		public static void DeletePost(Dictionary<string,string> Params,out FeedbackData msg) {
			msg = new FeedbackData();
			var id=Params["id"];
			var user = HttpContext.Current.User;
			if (user.Identity.IsAuthenticated)
			{
				Post post = Models.Post.GetPost(Convert.ToInt32(id));
				if (post == null) {
					msg.IsOk = false;
					msg.Body = "新闻不存在";
					msg.Context.Add("url",Utils.AbsoluteWebRoot.ToString());
					return;
				}

				if (user.IsInRole(PortalSettings.Instance.AdministratorRole) || user.Identity.Name == post.Author)
				{
					try
					{
						post.Delete();
						post.Save();
						msg.Body = "删除成功";
						msg.Context.Add("url", Utils.AbsoluteWebRoot.ToString());
					}
					catch (Exception ex){
						msg.IsOk = false;
						msg.Body = "删除时发生例外,请稍候重试";
						msg.Context.Add("error",ex.Message);
					}
				}
				else {
					msg.IsOk = false;
					msg.Body = "无权限";
				}
			}
			else {
				msg.IsOk = false;
				msg.Body = "登录信息丢失,请重新登录后执行删除操作";
				msg.Context.Add("url",Utils.AbsoluteWebRoot.ToString()+"login.aspx");
			}
		}
	}
}
